﻿<template>
    <el-dialog :title="titleMap[mode]" v-model="visible" :width="1400" destroy-on-close>
        <el-form
            :model="entityObj"
            :rules="rules"
            :disabled="mode == 'Show'"
            ref="dialogForm"
            label-width="100px"
        >
            <el-row style="margin-top: 24px;">
                <el-col :span="8">
                    <el-form-item label-width="120px" label="单据单号:">{{entityObj.afterOrderCode}}</el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label-width="120px" label="单据状态:">{{entityObj.statusDesc}}</el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label-width="120px" label="单据金额:">
                        <span style="color: red;">￥{{entityObj.totalAmount}}元</span>
                    </el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="8">
                    <el-form-item label-width="120px" label="售后单位:">
                        <span style="color: blue;">{{entityObj.storeName}}</span>
                    </el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label-width="120px" label="售后人:">{{entityObj.createName}}</el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label-width="120px" label="售后时间:">{{entityObj.createTime}}</el-form-item>
                </el-col>
            </el-row>

            <el-row>
                <el-col :span="8">
                    <el-form-item label-width="120px" label="审核备注:">{{entityObj.auditRemarks}}</el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label-width="120px" label="审核人:">{{entityObj.auditName}}</el-form-item>
                </el-col>
                <el-col :span="8">
                    <el-form-item label-width="120px" label="审核时间:">{{entityObj.auditDate}}</el-form-item>
                </el-col>
            </el-row>

            <el-row>
                <el-col :span="8">
                    <el-form-item label-width="120px" label="备注:">{{entityObj.remarks}}</el-form-item>
                </el-col>
            </el-row>
            <el-row>
                <el-col :span="24">
                    <el-form-item label-width="120px" label="售后清单:">
                        <el-table
                            :data="entityObj.afterOrderItems"
                            :border="true"
                            style="width: 100%"
                            :row-class-name="tableRowClassName"
                        >
                            <el-table-column label="供应商" prop="supplierName" width="180"></el-table-column>
                            <el-table-column label="物料编码" prop="materialCode" width="100px" />
                            <el-table-column label="物料名称" prop="materialName" />
                            <el-table-column label="分类" prop="materialTypeName" width="60px" />
                            <el-table-column label="类型" prop="afterOrderType" width="80px">
                                <template #default="scope">
                                    <el-tag
                                        type="primary"
                                        effect="dark"
                                        v-if="scope.row.afterOrderType==0"
                                    >补发</el-tag>
                                    <el-tag
                                        type="danger"
                                        effect="dark"
                                        v-if="scope.row.afterOrderType==1"
                                    >退货</el-tag>
                                </template>
                            </el-table-column>
                            <el-table-column label="规格" prop="materialSpecs" width="100px" />
                            <el-table-column label="单位" prop="unitName" width="60px" />
                            <el-table-column label="数量" prop="qty" width="80px" />
                            <el-table-column label="单价" prop="price" width="80px" />
                            <el-table-column label="金额" prop="totalAmount" width="80px" />
                            <el-table-column label="破损照片" width="100">
                                <template #default="scope">
                                    <el-image
                                        style="width: 50px; height: 50px"
                                        :src="scope.row.images"
                                        :fit="fit"
                                    />
                                </template>
                            </el-table-column>
                            <el-table-column label="备注" prop="remarks" width="100px" />
                        </el-table>
                    </el-form-item>
                </el-col>
            </el-row>
        </el-form>
    </el-dialog>
</template>



<script>
import tool from "@/utils/tool";
export default {
    emits: ["success", "closed"],
    data() {
        return {
            tableRowClassName: "warning-row",
            tableRowClassName1: "success-row",
            mode: "Add",
            titleMap: {
                Add: "新增",
                Edit: "编辑",
                Show: "查看",
            },
            entityObj: {
                purchaseProtocolId: 0,
                purchaseProtocolCode: "",
                purchaseProtocolName: "",
                status: 1,
                crName: "",
                crDate: "",
                supplier: {},
                protocolDate: [],
                purchaseUnityName: "",
                remarks: "",
                protocolFile: "",
                purchaseProtocolItems: [],
                giveOrderItems: [],
            },
            materialState: false,
            queryList: {},
            visible: false,
            isSaveing: false,
            needSaveFlag: false,

            props: {
                label: "supplierName",
                value: "supplierId",
            },
            materialProps: {
                label: "materialCode",
                value: "materialId",
                items: "materialItems",
            },
            materialTypeTree: [],
            optionsBrand: [],
            optionsUnit: [],
            optionsPurchaseSaleType: [],
            suppliers: [],
        };
    },
    computed: {},
    methods: {
        handleBeforeClose() {
            this.$confirm("信息未保存,确定要关闭吗？", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(() => {
                    this.visible = false;
                })
                .catch(() => {
                    // 用户点击了取消按钮，不执行关闭
                });
        },
        //显示=
        open(mode = "Add") {
            this.mode = mode;
            this.visible = true;
            return this;
        },
        //表单注入数据
        async setData(data) {
            this.entityObj = { ...data };
            if (!data.afterOrderCode) {
                try {
                    var returnValue = await this.$API.auth.GetProcessApi.get(
                        `after-order/${data.afterOrderId}`,
                        { }
                    );
                    if (returnValue.status == 200) {
                        this.entityObj = { ...returnValue.response };
                    } else {
                        this.$message.error("操作失败:" + returnValue.msg);
                    }
                } catch (error) {
                    this.loading = false;
                    return false;
                }
            }
        },
        async initOptions() {
            try {
                var returnValue = await this.$API.auth.PostProcessApi.post(
                    "base-dictionary/options",
                    { Codes: ["PurchaseSaleType"] }
                );
                if (returnValue.status == 200) {
                    this.optionsPurchaseSaleType = returnValue.response.find(
                        (p) => p.code == "PurchaseSaleType"
                    ).optionsDetails;
                } else {
                    this.$message.error("操作失败:" + returnValue.msg);
                }
            } catch (error) {
                this.loading = false;
                return false;
            }
        },
        formatDate(date) {
            const year = date.getFullYear();
            const month = String(date.getMonth() + 1).padStart(2, "0");
            const day = String(date.getDate()).padStart(2, "0");
            const hours = String(date.getHours()).padStart(2, "0");
            const minutes = String(date.getMinutes()).padStart(2, "0");
            const seconds = String(date.getSeconds()).padStart(2, "0");

            return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
        },
    },
    async mounted() {
        this.entityObj.purchaseUnityName = tool.data.get("USER_INFO").OrgName;
        this.entityObj.crName = tool.data.get("USER_INFO").Name;
        this.entityObj.crDate = this.formatDate(new Date());
        await this.initOptions();
    },
};
</script>

<style scoped>
.el-table .warning-row {
    --el-table-tr-bg-color: var(--el-color-warning-light-9);
}
.el-table .success-row {
    --el-table-tr-bg-color: var(--el-color-success-light-9);
}
.el-form-item {
    margin-bottom: 8px !important;
}
</style>
